package com.songlanyun.modules.marketing.groupbuy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.modules.exception.MarketingException;
import com.songlanyun.modules.marketing.groupbuy.model.dto.GroupBuyActivityDTO;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityGoodsService;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivityTimeItem;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.marketing.groupbuy.dao.GroupBuyActivityDao;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyActivity;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("groupBuyActivityService")
public class GroupBuyActivityServiceImpl extends ServiceImpl<GroupBuyActivityDao, GroupBuyActivity> implements GroupBuyActivityService {

    @Resource
    private GroupBuyActivityGoodsService groupBuyActivityGoodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<GroupBuyActivity> page = this.baseMapper.groupBuyPage(new Query<GroupBuyActivity>().getPage(params), params);

        return new PageUtils(page);
    }

    /**
     * 新建拼团
     *
     * @param dto
     */
    @Override
    @Transactional
    public GroupBuyActivity saveGroupBuy(GroupBuyActivityDTO dto) {
        //保存拼团活动
        GroupBuyActivity groupBuyActivity = new GroupBuyActivity();
        BeanUtil.copyPropertiesIgnoreNull(dto, groupBuyActivity);

        //判断该商家在日期时间内有没有创建过活动，创建过则不能在创建
        verifyGroupBuyActivity(groupBuyActivity);
        this.baseMapper.insert(groupBuyActivity);

        return groupBuyActivity;

    }

    @Override
    public void updateGroupBuy(GroupBuyActivityDTO dto) {
        //更新拼团活动
        GroupBuyActivity groupBuyActivity = this.baseMapper.selectById(dto.getId());//检验状态
        BeanUtil.copyPropertiesIgnoreNull(dto, groupBuyActivity);//拷贝

        //判断该商家在日期时间内有没有创建过活动，创建过则不能在创建
        verifyGroupBuyActivity(groupBuyActivity);
        this.baseMapper.updateById(groupBuyActivity);
    }

    /**
     * 校验拼团活动时间
     *
     * @param activity
     */
    private void verifyGroupBuyActivity(GroupBuyActivity activity) {
        //如果开始时间和结束时间都不在之前记录的时间范围，则可以创建
        int count = this.baseMapper.verifyGroupBuyActivity(activity);

        if (count > 0) {
            throw new RRException(MarketingException.GROUP_BUY_ACTIVITY_TIME_USEING);
        }
    }


    @Override
    public GroupBuyActivity getById(Long id, boolean isThrow) {

        GroupBuyActivity groupBuyActivity = this.baseMapper.selectById(id);
        if (groupBuyActivity == null) {
            if (isThrow) {
                throw new RRException(MarketingException.GROUP_BUY_ACTIVITY_NOT_EXIST);
            }
        }
        return groupBuyActivity;
    }

    @Override
    public GroupBuyActivity getByShopIdAndId(Long shopId, Long groupBuyId, boolean b) {
        GroupBuyActivity one = this.baseMapper.selectOne(
                new LambdaQueryWrapper<GroupBuyActivity>()
                        .eq(GroupBuyActivity::getId, groupBuyId)
                        .eq(GroupBuyActivity::getShopId, shopId)
                        .last("limit 1")
        );
        if (one == null) {
            if (b) {
                throw new RRException(MarketingException.SECKILL_ACTIVITY_NOT_EXIST);
            }
        }
        return one;
    }

    @Override
    public Long finishTime(Long shopId) {
        Long finishTime = 0L; //结束时间秒值

        GroupBuyActivity groupBuyActivity = this.baseMapper.getNowShopActivity(shopId);
        if (groupBuyActivity != null) {
            //结束一天，需要加一天
            Date endTime = groupBuyActivity.getEndTime();//
            Date date = DateUtils.addDateDays(endTime, 1);
            if (date != null) {
                finishTime = date.getTime() / 1000;
            }
        }

        return finishTime;
    }

    @Override
    public GroupBuyActivity getNow() {
        return this.baseMapper.getNow();
    }

    @Override
    @Transactional
    public void delById(Long id) {
        //校验拼团中是否有拼团商品
        groupBuyActivityGoodsService.verifyActiveGoods(id);
        //删除
        this.baseMapper.deleteById(id);
    }


}
